package com.freeLearning.boot.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.freeLearning.boot.common.annotation.Log;
import com.freeLearning.boot.common.enums.LogModuleEnum;
import com.freeLearning.boot.common.result.Result;
import com.freeLearning.boot.system.model.entity.Region;
import com.freeLearning.boot.system.model.form.RegionForm;
import com.freeLearning.boot.system.model.vo.RegionVO;
import com.freeLearning.boot.system.service.RegionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "14.地区接口")
@RestController
@RequestMapping("/api/v1/region")
@RequiredArgsConstructor
public class RegionController {

    private final RegionService regionService;

    @Operation(summary = "地区列表")
    @GetMapping
    @Log( value = "地区列表",module = LogModuleEnum.REGION)
    public Result<List<RegionVO>> getRegionList(){
        List<RegionVO> list = this.regionService.getRegionList();
        return Result.success(list);
    }
    @Operation(summary = "所以地区列表")
    @GetMapping("/getAllRegion")
    @Cacheable(value = "region", key = "2",sync = true)
    @Log( value = "所以地区列表",module = LogModuleEnum.REGION)
    public Result<List<Region>> getAllRegion(){
        return Result.success(this.regionService.list());
    }

    @Operation(summary = "根据级别获取地区列表")
    @PostMapping("/getByLevelAndParentId")
    @Log( value = "根据level查询地区",module = LogModuleEnum.REGION)
    public Result<List<Region>> getRegionByLevel(
            @RequestBody Region region
    ){
        List<Region> list = this.regionService.list(
                new QueryWrapper<Region>()
                        .eq(region.getLevel() != null,"level", region.getLevel())
                        .eq(region.getParentId() != null, "parent_id", region.getParentId())
        );
        return Result.success(list);
    }
    @Operation(summary = "根据id查询地区")
    @Log( value = "根据id查询地区",module = LogModuleEnum.REGION)
    @GetMapping("/getRegionById")
    public Result<Region> getRegionById(
            @RequestParam(required = false) Long id
    ){
        if (id == -1){
            id = null;
        }
        Region region = this.regionService.getById(id);
        return Result.success(region);
    }

    @Operation(summary = "新增地区")
    @PostMapping
    @Log(value = "新增地区",module = LogModuleEnum.REGION)
    @CacheEvict(value = "region",allEntries = true)
    public Result<Boolean> addRegion(@RequestBody RegionForm regionForm){
        Region region = BeanUtil.copyProperties(regionForm, Region.class);
        boolean status = this.regionService.save(region);
        return  Result.judge(status);
    }

    @Operation(summary = "修改地区")
    @PutMapping
    @Log(value = "修改地区",module = LogModuleEnum.REGION)
    @CacheEvict(value = "region",allEntries = true)
    public Result<Boolean> updateRegion(@RequestBody RegionForm regionForm){
        Region region = BeanUtil.copyProperties(regionForm, Region.class);
        boolean status = this.regionService.updateById(region);
        return  Result.judge(status);
    }

    @Operation(summary = "删除地区")
    @DeleteMapping
    @Log(value = "删除地区",module = LogModuleEnum.REGION)
    @CacheEvict(value = "region",allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> deleteRegion(Long id){
        boolean status = this.regionService.removeById(id);
        boolean cStatus = this.regionService.remove(new QueryWrapper<Region>().eq("parent_id", id));
        return Result.success();
    }
}
